// +----------------------------------------------------------------------


// +----------------------------------------------------------------------
const conversation={
	//保存的键名
	key:'uni_user_im_conversation',
	//临时会话保存数量
	number:300,
	//保存会话
	setSation:(list,idKey)=>{
		let data=conversation.getSation();
		let tempArr={};
		for(let i in list){
			tempArr={};
			for(let j in data){
				if(list[i][idKey] == data[j][idKey]){
					tempArr=list[i];
					tempArr['time']=parseInt(new Date().getTime() / 1000);
					data[j]=tempArr;
					break;
				}
			}
			if(!tempArr.time){
				list[i].time=parseInt(new Date().getTime() / 1000);
				data.push(list[i]);
			}
		}
		uni.setStorageSync(conversation.key, data);
	},
	//获取会话
	getSation:(idVal='',idKey='')=>{
		let cache = uni.getStorageSync(conversation.key);
		if(!cache) cache=[];
		if(idVal) {
			for(let i in cache){
				if(idVal == cache[i][idKey]){
					return cache[i];
				}
			}
		}
		let newCache=[];
		cache=conversation.sortByTimeDesc(cache);
		for(let i in cache){
			newCache[i]=cache[i];
			if(i >= (conversation.number-1)) break;
		}
		return cache;
	},
	//按时间排序
	sortByTimeDesc:(arr)=>{
		return arr.sort((a, b) => b.time - a.time);
	},
};
uni.$conversation=conversation;

